<template>
  <div class="create-store-container">
    <el-card class="store-form-card">
      <template #header>
        <div class="card-header">
          <span class="card-title">创建商店</span>
        </div>
      </template>

      <el-form :model="storeForm" label-width="120px" @submit.prevent="submitForm">
        <!-- 商店名称 -->
        <el-form-item label="商店名称" required>
          <el-input v-model="storeForm.name" placeholder="请输入商店名称" />
        </el-form-item>

        <!-- 商店描述 -->
        <el-form-item label="商店描述" required>
          <el-input v-model="storeForm.description" type="textarea" :rows="3" placeholder="请输入商店描述" />
        </el-form-item>

        <el-form-item label="商店所有人" required>
          <el-input v-model="storeForm.owner" placeholder="请输入商店所有人姓名" />
        </el-form-item>

        <el-form-item label="商店经营人" required>
          <el-input v-model="storeForm.operator" placeholder="请输入商店实际经营人姓名" />
        </el-form-item>


        <!-- 商店联系方式 -->
        <el-form-item label="商店联系方式" required>
          <el-input v-model="storeForm.contactNumber" placeholder="请输入商店联系方式" />
        </el-form-item>

        <!-- 商店经营时间 -->
        <el-form-item label="商店经营时间" required>
          <el-input v-model="storeForm.operatingHours" placeholder="请输入商店经营时间" />
        </el-form-item>

              <!-- 商店位置类型 -->
              <el-form-item label="商店位置类型" required>
          <el-select v-model="storeForm.storeFormat" placeholder="请选择商店类型">
            <el-option label="流动" :value=1 />
            <el-option label="固定" :value=2 />
            <el-option label="其他" :value=3 />
          </el-select>
        </el-form-item>

        <!-- 商店类型 -->
        <el-form-item label="商店商品类型" required>
          <el-select v-model="storeForm.storeType" placeholder="请选择商店类型">
            <el-option label="食品" :value="1" />
            <el-option label="服装" :value="2" />
            <el-option label="电子产品" :value="3" />
            <el-option label="书籍" :value="4" />
            <el-option label="其他" :value="5" />
          </el-select>
        </el-form-item>

        <!-- 商店地址 -->
        <el-form-item label="请选择商店地点" prop="location">
          <el-select v-model="storeForm.facilityId" placeholder="请选择商店地点" @change="onFacilityChange">
            <el-option v-for="facility in facilities" :key="facility.id" :label="facility.name" :value="facility.id" />
          </el-select>
        </el-form-item>

        <!-- 商店图片 -->
        <el-form-item label="商店图片">
          <el-upload
            class="store-uploader"
            :show-file-list="false"
            :before-upload="beforeUpload"
            @success="handleSuccess"
            @error="handleError"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <div v-if="storeForm.image" class="uploaded-image">
            <img :src="storeForm.image" alt="上传的图片" style="max-width: 100%; margin-top: 10px;" />
          </div>
        </el-form-item>

        <!-- 提交按钮 -->
        <el-form-item>
          <el-button type="primary" native-type="submit">创建商店</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { listFacilities } from '@/api/facility';
import { uploadImage } from "@/api/upload";
import { ElMessage } from 'element-plus'; 
import {createStore} from '@/api/virtualStore';
import { useRouter } from 'vue-router';
const router = useRouter();
const storeForm = ref({
  name: '',
  description: '',
  location: '',
  contactNumber: '',
  operatingHours: '',
  storeType: '', // 添加商店类型字段
  facilityId: '',
  image: null,
});

//

const facilities = ref([]);

const getFacilities = async () => {
  const res = await listFacilities();
  if (res.code === 200) {
    facilities.value = res.data;
  } else {
    ElMessage.error('获取设施列表失败');
  }
};

const onFacilityChange = (selectedId) => {
  const selectedFacility = facilities.value.find(facility => facility.id === selectedId);
  storeForm.value.location = selectedFacility ? selectedFacility.name : '';
};

const beforeUpload = (file) => {
  uploadImage(file).then(data => {
    storeForm.value.image = data; 
  });
  return true; 
};

const handleSuccess = (response) => {
  storeForm.value.image = response; 
};

const handleError = (err) => {
  console.error("上传失败:", err);
};

const submitForm = () => {
  if (!storeForm.value.name || !storeForm.value.description || !storeForm.value.location || !storeForm.value.storeType) {
    ElMessage.error('请填写完整信息');
    return;
  }

  const params = {
    name: storeForm.value.name,
    description: storeForm.value.description,
    contactNumber: storeForm.value.contactNumber,
    operatingHours: storeForm.value.operatingHours,
    storeType: storeForm.value.storeType,
    facilityId: storeForm.value.facilityId,
    location: storeForm.value.location,
    image: storeForm.value.image,
    owner: storeForm.value.owner,
    operator: storeForm.value.operator,
    storeFormat: storeForm.value.storeFormat,
  };

  createStore(params).then(res => {
    if (res.code === 200) {
      ElMessage.success('商店创建成功！');
      resetForm();
      router.push('/store');
    } else {
      ElMessage.error('商店创建失败！');
    }
  });
};

const resetForm = () => {
  storeForm.value = {
    name: '',
    description: '',
    contactNumber: '',
    operatingHours: '',
    storeType: '',
    facilityId: '',
    image: null,
  };
};

onMounted(() => {
  getFacilities();
});
</script>

<style scoped>
.create-store-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 95vw;
  height: auto;
  background-color: #f5f7fa;
}

.store-form-card {
  width: 100%;
  max-width: 500px;
  padding: 20px;
}

.card-header {
  text-align: center;
}

.card-title {
  font-size: 1.5rem;
  font-weight: bold;
}
</style>